#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <limits>
#include <iomanip>
#include "courseLib.h"
#include "Student.h"

using namespace std;

int main() {
    // Create course libraries and student instances
    CourseLib courseLibrary;
    Student s1;
    s1.initialSchedule();

    // Attempt to load course data, initialize course library if file does not exist
    if (ifstream("course_info.txt").good()) {
        courseLibrary.loadCoursesFromFile();
    } else {
        courseLibrary.initCourseLib();
        courseLibrary.saveCoursesToFile();
    }

    int choice;
    int exit = 1;
    do {
        cout << "\n=== Course Management System ===" << endl;
        cout << "1. Add a course" << endl;
        cout << "2. Display all courses" << endl;
        cout << "3. Filter courses" << endl;
        cout << "4. Delete a course" << endl;
        cout << "5. Modify course nature" << endl;
        cout << "6. Print the schedule" << endl;
        cout << "7. Select a course" << endl;
        cout << "8. Drop a course" << endl;
        cout << "0. Exit" << endl;
        cout << "Enter your choice: ";
        cin >> choice;
        if (cin.fail()) {
            cout << "Invalid input! Please enter a valid number." << endl;
            cin.clear();
            while (cin.get() != '\n'){};
            choice = 999;
            continue;
        } else {
            while (cin.get() != '\n'){};
        }
		
        switch (choice) {
        case 1:
            // Add course and save to file
            courseLibrary.addCourse();
            courseLibrary.saveCoursesToFile();
            break;
        case 2:
            // Show all courses
            courseLibrary.displayAllCourses();
            break;
        case 3:
            // Filtering Courses
            courseLibrary.filterCourses();
            break;
        case 4:
            // Delete the course and save it to a file
            courseLibrary.deleteCourse();
            courseLibrary.saveCoursesToFile();
            break;
        case 5:
            // Modify course credits and save to file
            courseLibrary.modifyNature();
            courseLibrary.saveCoursesToFile();
            break;
        case 6:
            // Print the current class schedule
            s1.printSchedule();
            break;
        case 7:
            // Show all courses and select a course
            courseLibrary.displayAllCourses();
            s1.selectCourse(courseLibrary);
            break;
        case 8:
            // Show all courses and select a course
            s1.dropCourse(courseLibrary);
            break;
        case 0:
            // Exit the system if there are enough credits
            if(s1.isCreditSufficient(s1.calculateTotalCredits())){
            	cout << "Exiting the system. Goodbye!" << endl;
            	exit = 0;
			}else{
				cout << "You still need more credits."<< endl;
			}
            break;
        default:
            cout << "Invalid choice. Please try again." << endl;
            break;
        }
    } while (exit != 0);

    return 0;
}

